//
//  RightMenuViewController.m
//  51-Tour
//
//  Created by xiang ying on 14-3-18.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "RightMenuViewController.h"
#import "UserDTO.h"
#import "SVProgressHUD.h"

@interface RightMenuViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSArray *_itemsArray,*_imagesArray;
    UITableView *menu;
    UIButton *logoutBtn;
}

@end

@implementation RightMenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageReset:) name:@"RESETLANGUAGE" object:nil];

    }
    return self;
}

- (void)languageReset:(NSNotification *)noti{
    _itemsArray = @[[[DataCenter instance] getStringForKey:@"RecommendNum"],
                    [[DataCenter instance] getStringForKey:@"PersonInfo"],
                    [[DataCenter instance] getStringForKey:@"History"],
                    [[DataCenter instance] getStringForKey:@"Record"]];
    [logoutBtn setTitle:[[DataCenter instance] getStringForKey:@"LogOut"] forState:UIControlStateNormal];
    [menu reloadData];
}

- (void)viewWillAppear:(BOOL)animated{
    [menu reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _itemsArray = @[[[DataCenter instance] getStringForKey:@"RecommendNum"],
                    [[DataCenter instance] getStringForKey:@"PersonInfo"],
                    [[DataCenter instance] getStringForKey:@"History"],
                    [[DataCenter instance] getStringForKey:@"Record"]];
    _imagesArray = @[@"ic-like_06",@"ico_myinfo",@"ic-news_13",@"ic-record_13"];
    
    if (IOS7) {
        menu = [[UITableView alloc] initWithFrame:CGRectMake(0, self.contentView.frame.origin.y-44, 320, self.contentView.frame.size.height+44)];
    }else{
        menu = [[UITableView alloc] initWithFrame:self.contentView.frame];
    }
    menu.separatorStyle = UITableViewCellSeparatorStyleNone;
    [menu setBackgroundColor:[UIColor clearColor]];
    menu.dataSource = self;
    menu.delegate = self;
    [self.view addSubview:menu];

    logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [logoutBtn setBackgroundColor:[UIColor orangeColor]];
    [logoutBtn addTarget:self action:@selector(userlogout:) forControlEvents:UIControlEventTouchUpInside];
    logoutBtn.frame = CGRectMake((260-200)/2, menu.frame.size.height-100, 200, 40);
    [logoutBtn setTitle:[[DataCenter instance] getStringForKey:@"LogOut"] forState:UIControlStateNormal];
    [self.view addSubview:logoutBtn];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _itemsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        
        UIImageView *_line = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list-line_14"]];
        _line.frame = CGRectMake(0, 43, 320, 1);
        [cell addSubview:_line];
    }
    cell.textLabel.text = _itemsArray[indexPath.row];
    if (indexPath.row == 0) {
        cell.textLabel.text = [_itemsArray[indexPath.row] stringByAppendingFormat:@" %@",[DataCenter instance].user.recommendNum];
    }
    cell.imageView.image = [UIImage imageNamed:_imagesArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:{
            //
            
        }
            
            break;
            
        case 1:{
            //我的资料
            [[UIManagement shareInstance] showPersoninfo];
        }
            break;
            
        case 2:{
            //历史消息
            [[UIManagement shareInstance] showHistory];
            
        }
            break;
            
        case 3:{
            //讲解记录
            [[UIManagement shareInstance] showRecord];
            
        }
            
        default:
            break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorWithRed:214.0/255.0 green:243.0/255.0 blue:252.0/255.0 alpha:1];
    
    UIImage *image = [DataCenter instance].user.userIcon;
    if (!image) {
        image = [UIImage imageNamed:@"default-avatar_05"];
    }
    UIImageView *head = [[UIImageView alloc] initWithImage:image];
    head.frame = CGRectMake(20, 7, 30, 30);
    [view addSubview:head];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 320-60, 44)];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = THEMECOLOR;
    label.font = [UIFont systemFontOfSize:15];
    label.text = [DataCenter instance].user.userPhone;
    [view addSubview:label];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}

-(void)userlogout:(id)sender{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[DataCenter instance] getStringForKey:@"Tips"] message:[[DataCenter instance] getStringForKey:@"vtg_logout_confirm"] delegate:self cancelButtonTitle:[[DataCenter instance] getStringForKey:@"cancel"] otherButtonTitles:[[DataCenter instance] getStringForKey:@"Sure"], nil];
    [alert show];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [[DataCenter instance] setUserInfo:nil];
        [[UIManagement shareInstance] showCenter];
        //    [SVProgressHUD showSuccessWithStatus:@"退出登录成功"];
    }
}

@end
